package com.csii.core.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.security.SecureRandom;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class PmsUtil {


    private static String uploadPath;

    @Value("${stdio.path.upload:''}")
    public void setUploadPath(String uploadPath) {
        PmsUtil.uploadPath = uploadPath;
    }

    public static String saveErrorTxtByList(List<String> msg, String name) {
        Date d = new Date();
        String saveDir = "logs" + File.separator + DateUtils.yyyyMMdd.get().format(d) + File.separator;
        String saveFullDir = uploadPath + File.separator + saveDir;

        File saveFile = new File(saveFullDir);
        if (!saveFile.exists()) {
            saveFile.mkdirs();
        }

        try {
            //将java.util.Random替换成java.security.SecureRandom
            //name += DateUtils.yyyymmddhhmmss.get().format(d) + Math.round(Math.random() * 10000);
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            name += DateUtils.yyyymmddhhmmss.get().format(d) + Math.round(random.nextFloat() * 10000);
        } catch(Exception e) {
            e.printStackTrace();
        }

        String saveFilePath = saveFullDir + name + ".txt";

            //封装目的地
         try (BufferedWriter bw = new BufferedWriter(new FileWriter(saveFilePath))) {
            //遍历集合
            for (String s : msg) {
                //写数据
                if (s.indexOf("_") > 0) {
                    String arr[] = s.split("_");
                    bw.write("第" + arr[0] + "行:" + arr[1]);
                } else {
                    bw.write(s);
                }
                //bw.newLine();
                bw.write("\r\n");
            }
            //释放资源
            bw.flush();
            bw.close();
        } catch (Exception e) {
            log.info("excel导入生成错误日志文件异常:" + e.getMessage());
        }
        return saveDir + name + ".txt";
    }

}
